{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3d374bff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python 3.10.0\r\n"
     ]
    }
   ],
   "source": [
    "!python3 --version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d6e1cf49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: http://mirrors.aliyun.com/pypi/simple/\n",
      "Collecting pandas\n",
      "  Downloading http://mirrors.aliyun.com/pypi/packages/58/58/b729eda34f78060e14cb430c91d4f7ba3cf1e34797976877a3a1125ea5b2/pandas-1.3.4.tar.gz (4.7 MB)\n",
      "\u001b[K     |████████████████████████████████| 4.7 MB 379 kB/s eta 0:00:011\n",
      "\u001b[?25h  Installing build dependencies ... \u001b[?25ldone\n",
      "\u001b[?25h  Getting requirements to build wheel ... \u001b[?25l-^C\n",
      "\u001b[?25canceled\n",
      "\u001b[31mERROR: Operation cancelled by user\u001b[0m\n",
      "\u001b[33mWARNING: You are using pip version 21.2.3; however, version 21.3.1 is available.\n",
      "You should consider upgrading via the '/Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10 -m pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "# 导入一个包之前先需要安装\n",
    "!pip3 install pandas --user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "11ab7fe7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# python里面的import from...import...\n",
    "import pandas as pd  \n",
    "from pandas import DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b0003ee8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b362fa02",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 以下代码来自：https://blog.csdn.net/fqfq123456/article/details/115766897\n",
    "# 仅做学习交流\n",
    "import base64\n",
    "import pandas as pd\n",
    "from pandas.core.frame import DataFrame\n",
    "\n",
    "# 目录下所有excel换题头\n",
    "def muHuanTitou(path, biaotou):\n",
    "    df = pd.read_excel(path, None)\n",
    "    # 替换Excel 表格内的空单元格，否则在下一步处理中将会报错\n",
    "    import  datetime\n",
    "    str_p = datetime.datetime.now()\n",
    "    dateTime_p = datetime.datetime.strftime(str_p, '%H%M%S')\n",
    "    writer = pd.ExcelWriter('N'+path[:-4]+str(dateTime_p)+'.xlsx')\n",
    "    for sh in df.keys():\n",
    "        fdf = pd.read_excel(path, sh)\n",
    "        df_list=[]\n",
    "        fdf.fillna(\"\", inplace=True)\n",
    "        sjbt=fdf.columns.to_list()\n",
    "        if len(sjbt)>0:\n",
    "            bt=biaotou\n",
    "            xuadd=0\n",
    "            if len(sjbt)==len(biaotou):\n",
    "                bt=biaotou\n",
    "            elif len(sjbt)>len(biaotou):\n",
    "                for i in range(len(sjbt)-len(biaotou)):\n",
    "                    bt.append('bt'+str(i))\n",
    "            else:\n",
    "                bt=biaotou\n",
    "                xuadd = len(biaotou)-len(sjbt)\n",
    "\n",
    "            for i in fdf.index.values:\n",
    "                # loc为按列名索引 iloc 为按位置索引，使用的是 [[行号], [列名]]\n",
    "                df_line = fdf.loc[i, fdf.columns].to_dict()\n",
    "                # print(df_line)\n",
    "                for i in range(xuadd):\n",
    "                    newl='bt'+str(i)\n",
    "                    df_line[newl]=''\n",
    "                # if\n",
    "                # df_line['行业'] = sh  # excel的sheet表名为行业 + ';' + df_line['行业']\n",
    "                # 将每一行转换成字典后添加到列表\n",
    "                # if df_line['地区'] != '':\n",
    "                df_list.append(df_line)\n",
    "            nydf = DataFrame(df_list)\n",
    "            nydf.to_excel(excel_writer=writer, sheet_name=sh, header=biaotou)\n",
    "            # print(len(df_list))\n",
    "    writer.save()\n",
    "    writer.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "aff1dfcb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'123'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a590b22",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6f5dd85d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hhhhh\\naaa'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = '''hhhhh\n",
    "aaa'''\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "81361e05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(b)\n",
    "# 怎么查看一个变量的数据类型：type() 参数：要查看的变量名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4c36b973",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 代码格式的详解\n",
    "def find_ab(a, b):\n",
    "    flag = 0\n",
    "    for i in range(len(a)):\n",
    "        for j in range(len(b)):\n",
    "            # print(i ,j , len(b)-1, b[j], a[i+j])\n",
    "            if b[j] == a[i+j]: \n",
    "                # 两个元素相等 且为b的最后一个元素，说明a包含b\n",
    "                if j == len(b)-1:\n",
    "                    return True\n",
    "                # 两个元素相等 但是还没遍历完b，则继续遍历\n",
    "                continue\n",
    "            else: # 两个元素不相等，直接往后遍历a再开始比较\n",
    "                break\n",
    "    # 没找到，返回False    \n",
    "    return False\n",
    "\n",
    "a = \"welc ometomy world\"\n",
    "b = \"worl\"\n",
    "find_ab(a, b)     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae65f319",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
